﻿using log4net;
using Mate.Caching;
using Mate.Core.Base;
using Mate.Core.Configuration;
using Mate.Dto.Merchant;
using Mate.Framework.BaseController.Api;
using Mate.Merchant.Api.Controller.Models.MerchantCategory;
using Mate.Merchant.Service;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mate.Merchant.Api.Controller.Controller
{
    public class MerchantCategoryController: MerchantBaseController
    {
        private readonly ILog log;
        private readonly ICacheManager cache;
        private readonly MateConfig mateConfig;
        private readonly IMerchantCategoryService merchantCategoryService;

        public MerchantCategoryController(ILog log, ICacheManager cache, MateConfig mateConfig, IMerchantCategoryService merchantCategoryService)
        {
            this.log = log;
            this.cache = cache;
            this.mateConfig = mateConfig;
            this.merchantCategoryService = merchantCategoryService;
        }

        #region 获取首页商户分类
        [HttpPost]
        public BaseResult<List<HomeMcModel>> HomeMc(HomeMcRequest req)
        {
            BaseResult<List<HomeMcModel>> result = new BaseResult<List<HomeMcModel>>();

            string key = "mate.app.home.merchant.category.list";
            var cacheList = cache.Get<List<HomeMcModel>>(key);
            if (cacheList != null)
            {
                result.ResultCode = ResultCode.Success;
                result.Message = "OK";
                result.Data = cacheList;
                return result;
            }

            var dataList = merchantCategoryService.Queryable
                .Where(x => x.is_delete == 1 && x.is_valid == 1 && x.parentid == 0)
                .ToList();
            if (dataList == null)
            {
                result.ResultCode = ResultCode.Success;
                result.Message = "暂无相关数据";
                return result;
            }

            result.ResultCode = ResultCode.Success;
            result.Message = "OK";
            result.Data = dataList.Select(x => new HomeMcModel()
            {
                id=1,
                merchant_typeid=x.id,
                merchat_typename=x.merchantcategoryname
            }).Take(req.top).ToList();
            cache.Set(key, result.Data, mateConfig.DefaultCacheTime);

            return result;
        }
        #endregion

        #region 商品分类Tree
        /// <summary>
        /// 商品分类Tree
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public BaseResult<IList<MerchantCategoryTreeModel>> Tree(TreeRequest req)
        {
            BaseResult<IList<MerchantCategoryTreeModel>> result = new BaseResult<IList<MerchantCategoryTreeModel>>();
            result.ResultCode = ResultCode.Success;
            result.Message = "OK";
            result.Data = merchantCategoryService.GetMerchantCategoryTree(1,req.merchantcategoryid);
            return result;
        }
        #endregion

        #region 查询热门商户类型
        /// <summary>
        /// 查询热门商户类型
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public BaseResult<IList<HotMerchantCategoryDto>> HotMerchantCategory(HotMerchantCategoryRequest req)
        {
            BaseResult<IList<HotMerchantCategoryDto>> result = new BaseResult<IList<HotMerchantCategoryDto>>();
            result.ResultCode = ResultCode.Success;
            result.Message = "OK";
            result.Data = merchantCategoryService.GetHotMerchantCategoryList(req.merchantcategoryparentid,req.top);
            return result;
        }
        #endregion

    }
}
